package io.renren.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import io.renren.common.constant.Code;
import io.renren.common.exception.BizException;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.utils.UUIDGenerator;
import io.renren.modules.sys.dao.DicGroupDao;
import io.renren.modules.sys.entity.DicGroup;
import io.renren.modules.sys.entity.DicGroupDetail;
import io.renren.modules.sys.service.DicGroupDetailService;
import io.renren.modules.sys.service.DicGroupService;
import io.renren.modules.sys.vo.DicGroupTree;

@Service("dicGroupService")
public class DicGroupServiceImpl extends ServiceImpl<DicGroupDao, DicGroup> implements DicGroupService {
	@Autowired
	private DicGroupDetailService dicGroupDetailService;
	@Override
	public List<DicGroupTree> getDicGroupTree() {
		List<DicGroup> lists = this.selectList(null);
		List<DicGroupTree> lrts = new ArrayList<DicGroupTree>();
		DicGroupTree dgt = null;

		// 一级菜单
		for (DicGroup dt : lists) {
			if (dt.getParentId() == null || "".equals(dt.getParentId())) {
				dgt = new DicGroupTree();
				BeanUtils.copyProperties(dt, dgt);
				lrts.add(dgt);
			}

		}

		for (DicGroupTree gt : lrts) {
			gt.setChildren(getChildren(gt.getDicGpId(), lists));
		}
		return lrts;
	}
	
	/**
	 * 递归子节点
	 * @param parentId
	 * @param dgps
	 * @return
	 */
	private List<DicGroupTree> getChildren(String parentId, List<DicGroup> dgps) {
		List<DicGroupTree> dgts = new ArrayList<DicGroupTree>();
		DicGroupTree dgt = null;
		for (DicGroup d : dgps) {
			if (parentId.equals(d.getParentId())) {
				dgt = new DicGroupTree();
				BeanUtils.copyProperties(d, dgt);
				dgts.add(dgt);
			}
		}

		for (DicGroupTree lt : dgts) {
			// 递归
			lt.setChildren(getChildren(lt.getDicGpId(), dgps));
		}

		return dgts;
	}

	@Override
	public DicGroup saveDicGroup(DicGroup dicGroup) {
		
		if(dicGroup == null) {
			throw new BizException(Code.COODE_SYS_O1,"字典分组为空");
		}
		
		if(StringUtils.isEmpty(dicGroup.getCode()) || StringUtils.isEmpty(dicGroup.getName())) {
			throw new BizException(Code.COODE_SYS_O1,"名称和编码不能为空");
			
		}
		
		//查询是否存在
		DicGroup dg = this.selectOne(new EntityWrapper<DicGroup>().eq("code", dicGroup.getCode()));
		//
		if(dg  != null ) {
			throw new BizException(Code.COODE_SYS_O2,"数据重复");
		}
		
		dicGroup.setDicGpId(UUIDGenerator.getUUID());
		//上级ID不为空则查找父级字典
		if(StringUtils.isNotEmpty(dicGroup.getParentId())) {
			DicGroup parentDg = this.selectById(dicGroup.getParentId()); 
			dicGroup.setPath(StringUtils.isEmpty(parentDg.getPath())?dicGroup.getDicGpId():parentDg.getPath()+','+dicGroup.getDicGpId());
		}else {
			dicGroup.setPath(dicGroup.getDicGpId());
		}
		dicGroup.setCreater(ShiroUtils.getUserEntity().getUsername());
		dicGroup.setCreateUserId(ShiroUtils.getUserEntity().getUserId());
		dicGroup.setTenantId(ShiroUtils.getUserEntity().getTenantId());
		this.insert(dicGroup);
		return dicGroup;
	}

	@Override
	public void deleteDicGp(String dicGpId) {
		if(StringUtils.isBlank(dicGpId)) {
			throw new BizException(Code.COODE_SYS_O1, "字典分组ID为空");
		}
		//检测是否存在子节点
		int dgpcount = this.selectCount(new EntityWrapper<DicGroup>().like("path", dicGpId).and("dic_gp_id <> {0}", dicGpId));
		if(dgpcount != 0) {
			throw new BizException(Code.COODE_SYS_O3, "存在下级节点，请先删除下级节点");
		}
		//检测是否存在明细
		int gpds = dicGroupDetailService.selectCount(new EntityWrapper<DicGroupDetail>().eq("dic_gp_id", dicGpId));
		if(gpds != 0) {
			throw new BizException(Code.COODE_SYS_O3, "分组存在明细，请先删除明细");
		}
		
		this.deleteById(dicGpId);
		
	}

}
